﻿using QRCodeBluetooth.JobWork;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace QRCodeBluetooth
{
    public class JobSchedulerWork
    {
        public static async void Work()
        {
            //调度器工厂
            ISchedulerFactory Work = new StdSchedulerFactory();
            //调度器
            IScheduler schedulers_work = await Work.GetScheduler();
            await schedulers_work.GetJobGroupNames();
            /*-------------计划任务代码实现------------------*/
            //创建任务
            IJobDetail job_Work = JobBuilder.Create<DeviceStateJob>()
                .WithIdentity("TimeTrigger", "TimeGroup")
                .Build();
            //创建触发器 2点一分时被执行
            //ITrigger trigger9 = TriggerBuilder.Create().WithCronSchedule("0 01 02 * * ?").WithIdentity("TimeTriggerddd", "TimeGroupdd").Build();
            //每隔多久执行一次  这个是每隔多久执行一遍
            ITrigger trigger = TriggerBuilder.Create().WithIdentity("TimeTrigger", "TimeGroup").WithSimpleSchedule(t => t.WithIntervalInSeconds(60).RepeatForever()).Build();//间隔60秒执行一次任务
            //添加任务及触发器至调度器中
            await schedulers_work.ScheduleJob(job_Work, trigger);
            /*-------------计划任务代码实现------------------*/
            //启动
            await schedulers_work.Start();
        }

        public static async void CleanData()
        {
            //调度器工厂
            ISchedulerFactory Work = new StdSchedulerFactory();
            //调度器
            IScheduler schedulers_work = await Work.GetScheduler();
            await schedulers_work.GetJobGroupNames();
            /*-------------计划任务代码实现------------------*/
            //创建任务
            IJobDetail job_Work = JobBuilder.Create<DataCleanService>()
                .WithIdentity("TimeTrigger1", "TimeGroup1")
                .Build();
            //创建触发器 0点1分时被执行
            ITrigger trigger = TriggerBuilder.Create().WithCronSchedule("0 05 01 * * ?").WithIdentity("TimeTriggerddd1", "TimeGroupdd1").Build();
            //每隔多久执行一次  这个是每隔多久执行一遍
            //ITrigger trigger = TriggerBuilder.Create().WithIdentity("TimeTrigger", "TimeGroup").WithSimpleSchedule(t => t.WithIntervalInSeconds(60).RepeatForever()).Build();//间隔60秒执行一次任务
            //添加任务及触发器至调度器中
            await schedulers_work.ScheduleJob(job_Work, trigger);
            /*-------------计划任务代码实现------------------*/
            //启动
            await schedulers_work.Start();
        }
    }
}
